package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.commons.utile.BeanCopyUtil;
import com.woniuxy.commons.utile.Result;
import com.woniuxy.controller.dto.AccountDto;
import com.woniuxy.controller.form.UpdateRepairInfoForm;
import com.woniuxy.dao.RepairRepairerUnitDao;
import com.woniuxy.dao.model.RepairInfo;
import com.woniuxy.commons.exception.LateException;
import com.woniuxy.controller.dto.RepairLogDto;
import com.woniuxy.dao.model.RepairLog;
import com.woniuxy.dao.RepairLogDao;
import com.woniuxy.dao.model.RepairRepairerUnit;
import com.woniuxy.service.RepairInfoService;
import com.woniuxy.service.RepairLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 维修日志表 服务实现类
 * </p>
 *
 * @author Keep it simple
 * @since 2022-06-28
 */
@Service
public class RepairLogServiceImpl extends ServiceImpl<RepairLogDao, RepairLog> implements RepairLogService {

    @Resource
    private  RepairLogDao repairLogDao;


    @Override
    public List<RepairLogDto> get(String repairNum) {
        List<RepairLogDto> all = repairLogDao.all(repairNum);
        if (all==null)throw  new LateException(1005,"查询维修订单出错了");
        return all;
    }
    @Resource
    RepairLogService repairLogService;

    @Resource
    RepairInfoService repairInfoService;

    @Resource
    RepairRepairerUnitDao repairRepairerUnitDao;

    /**
     * 新增
     * @param updateRepairInfoForm
     * @return
     */
    @Override
    public Result updateRepairInfo(UpdateRepairInfoForm updateRepairInfoForm) {
        // 新增一条日志 新增repairLog表信息
        AccountDto principal = (AccountDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String userName = principal.getAccountName();
        Integer userId =principal.getId();
        RepairLog newLog = new RepairLog();
        newLog = BeanCopyUtil.copyOne(updateRepairInfoForm,RepairLog.class);
        newLog.setOperuser(userId);
        repairLogService.save(newLog);
        //修改维修单状态 修改repairInfo表信息
        RepairInfo newRepairInfo = new RepairInfo();
        newRepairInfo.setRepairnum(updateRepairInfoForm.getRepairnum());
        Integer repairState = updateRepairInfoForm.getRepairstate();
        newRepairInfo.setRepairstate(repairState);
        //新增不考虑
        //客服接单变为待派工
        if(repairState==2){
            //接待客服id
            newRepairInfo.setAcceptuser(userId);
            //接待时间
            newRepairInfo.setResponsetime(LocalDateTime.now());
        }
        //客服指定维修工，变为待接单
        else if(repairState==3){
            //不需要做任何事,在其他服务做
            System.out.println("制定了维修工");
        }
        //维修工接单 待接单变为维修中
        else if(repairState==4){
            System.out.println(userId);
            Integer repairerUnitId = repairRepairerUnitDao.findRepairerUnitId(userId);
            System.out.println(repairerUnitId);
            //承接单位-维修工的单位
            newRepairInfo.setRepairprojectId(repairerUnitId);
            //维修工id
            newRepairInfo.setRepairpersonid(userId);
            newRepairInfo.setIsconfirm(1);
        }
        //完成订单
        else if(repairState==5){
            newRepairInfo.setIsconfirm(0);
            //完成时间
            newRepairInfo.setCompletedate(LocalDateTime.now());
        }
        //转驳回
        else if(repairState==6){
            newRepairInfo.setIsconfirm(0);
        }
        System.out.println(newRepairInfo);
        boolean repairBool = repairInfoService.update(newRepairInfo, new UpdateWrapper<RepairInfo>().eq("repairNum", newRepairInfo.getRepairnum()));
        if (repairBool) return Result.instance();
        return Result.instance().setCode(500).setMessage("更新失败");
    }
}
